package game;

import java.awt.Point;
import java.util.List;
import java.util.ArrayList;
/**
 * This creates a rook piece and its valid movements.
 * 
 * @author Nicole Waldrum
 * @date Janaury 30, 2010
 * @version 1.0
 *
 * @author Mark Kelly
 * @date February 19, 2010
 * @version 2.0
 * 
 * @author Trevor Bastian
 * @date February 20, 2010
 * @version 2.0
 */
public class Rook extends Piece {
    /**
     * Creates a new instance of Rook
     * 
     * @param name the name of this Rook
     * @param position the initial position of this Rook
     * @param team the team that this Rook is on
     */
    public Rook(String name, Point position, Team team) {
        super(name, position, team);
    }
    
    /**
     * Creates a new rook piece
     * 
     * @param position the initial position of this Rook
     * @param team the team that this Rook is on
     */
    public Rook(Point position, Team team) {
    	this((team == Team.WHITE) ? "WR" : "BR", position, team);
    }

    /**
     * Assumes that 'isValidMove()' has been called and returns true first
     * {@inheritDoc}
     */
    @Override
    public List<Point> checkMovePattern(Point dest) {
        List<Point> ret = new ArrayList<Point>();
        Point p = this.getLocation();
        int dx = (int) ( dest . getX () - p . getX ());
        int dy = (int) ( dest . getY () - p . getY ());
        int i = ( Math . abs ( dx ) > Math . abs ( dy ) ) ? Math . abs ( dx ) : Math . abs ( dy );
        
        if ( dx != 0 )
            dx /= Math . abs ( dx );    // give dx a magnitude of 1
        if ( dy != 0 )
            dy /= Math . abs ( dy );    // give dy a magnitude of 1
        
        for ( int j = 0; j < i - 1; j++ ) {
            p = new Point ((((int) p . getX ()) + dx ), (((int) p . getY ()) + dy ));
            ret . add( p );
        }
        
        return ret;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isValidMove(Piece piece, Point dest) {
        Point start = piece.getLocation();
        if ( (int)( start . getX () - dest . getX ()) == 0 || (int)( start . getY () - dest . getY ()) == 0 )
            return super.isValidMove(piece, dest);

        return false;
    }
}
